package com.example.yk

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.yk.entity.BrandEntityResult
import com.example.yk.entity.ConditionEntityResult
import com.example.zk3.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

class MoreViewModel:BaseViewModel() {
    val  brandSuccess = MutableLiveData<BrandEntityResult>()
    val brandFail = MutableLiveData<String>()

    fun brand(){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Int>()
                map["groupId"] = 59
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                val result = apiServer.brand(body)
                brandSuccess.value = result
            }catch (e:Exception){
                brandFail.value = e.message
            }
        }
    }
    val success = MutableLiveData<ConditionEntityResult>()
    val fail = MutableLiveData<String>()
    fun conditon(map:MutableMap<String,Any>){
        viewModelScope.launch {
            try {
                map["groupId"] = 59
                map["limit"] = 10
                map["start"] = 1
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                val result = apiServer.condition(body)
                success.value = result
            }catch (e:Exception){
                fail.value = e.message
            }
        }

    }
}